#!/usr/bin/env python

import sys
import socket
import random
import struct

from scapy.all import *
from scapy.all import sendp, send, get_if_list, get_if_hwaddr, hexdump
from scapy.all import Packet
# from scapy.all import Ether, IP, UDP, TCP
# from myTunnel_header import MyTunnel
from scapy.layers.inet import IP, TCP
from scapy.layers.l2 import Ether
from scapy.layers.http import *
import time


def get_if():
    ifs = get_if_list()
    iface = None
    for i in get_if_list():
        if "eth0" in i:
            iface = i
            break;
    if not iface:
        print("cannot find eth0")
        exit(1)
    return iface


def main():
    print('sending_message')
    addr = '172.16.1.2'
    iface = get_if()
    # message = "mymessage"
    http = HTTPRequest(Method=b'GET', Path=b'beijing0/bjtu0000/yulei0000', Http_Version=b'HTTP/1.1')
    # print(http)
    # print("sending on interface {} to IP addr {}".format(iface, str(addr)))
    # pkt =  Ether(src=get_if_hwaddr(iface), dst='08:00:00:00:01:00')
    # print('Ether_done')
    ether = Ether(src=get_if_hwaddr(iface), dst='08:00:00:00:01:11', type=0x0800)
    ip = IP(dst=addr, proto=6)
    tcp = TCP(dport=1234, sport=random.randint(49152, 65535))
    pkt = ether / ip / tcp / http
    # pkt = pkt / IP(dst=addr) / message
    # print('IP_done')

    ether.show2()
    ip.show2()
    tcp.show2()
    http.show2()
    current_time = time.time()
    # pkt.show2()
    # print(int(round(current_time * 1000)))

    sendp(pkt, iface=iface, verbose=False)

    return pkt


if __name__ == '__main__':
    main()
